import util from "../../utils/util";
import {
    getTokenModel
} from "../../models/common.js";
//index.js
//获取应用实例
Page({
    data: {
        isIndex: false,
        scrollHeight: 0,
        isList: false,
        limit: 6,
        status: "unused",
        bgHeight: 0
    },
    //加载数据
    onLoad: function () {
        wx.showLoading({
            title: "加载中"
        });
        this.isUserInfo();
        this._scrollHeight();
        this._bgHeight()
    },
    //scrollHeight计算
    _scrollHeight() {
        const height = 440 + 105 + 120 + 60; //其他地方的高度
        const scrollHeight = util.scrollHeight(height);
        this.setData({
            scrollHeight
        });
    },
    _bgHeight() {
        const height = 440 + +40 + 120 + 40; 
        const bgHeight = util.scrollHeight(height);
        this.setData({
            bgHeight
        });
    },
    //判断该用户数据是否存在
    isUserInfo() {
        getTokenModel().then(res => {
            console.log('123')
            //获取失败
            if (res.errorCode) {
                return util.showToast(res.msg);
            }
            //获取成功token赋值
            util.token = res.data.token;
            console.log("token");
            console.log(util.token);
            // 如果有绑定了手机号，进入个人中心
            if (res.data.phone) {
                wx.setStorageSync("user", true);
                this.setData({
                    isIndex: true,
                    isList: true
                });
            } else {
                // 未绑定手机号，去绑定页面进行绑定
                wx.redirectTo({
                    url: "../login/login"
                });
            }
        });
    },
    //更多优惠券ff
    moreCoupon() {
        this.openList("../coupon/coupon")
    },
    //跳转订单列表
    clickOrderList() {
        this.setData({
            isList: !this.data.isList
        });
    },
    openOrderList() {
        this.openList("../orderList/orderList")
    },
    //点击跳转个人中心
    pageRun() {
        this.openList("../personal/personal")
    },
    openList(url, $this = this) {
        wx.navigateTo({
            url: url,
            success() {
                $this.setData({
                    isList: true
                });
            }
        });
    },
    //选择加油
    clickOil() {
        this.openList("../select/select")
    },
    // 分享
    onShareAppMessage() {}
});